package com.commerceE.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.commerceE.common.exception.ServiceException;
import com.commerceE.domain.Category;
import com.commerceE.domain.Page;
import com.commerceE.domain.Product_instance;
import com.commerceE.domain.Product_option_values;
import com.commerceE.domain.Products;
import com.commerceE.domain.User;
import com.commerceE.service.CategoryService;
import com.commerceE.service.ProductInstanceService;
import com.commerceE.service.ProductService;
import com.commerceE.util.Constant;
import com.commerceE.vo.ProductSearchVo;

@SessionAttributes({"currentUser"})
@Controller
@RequestMapping("/product")
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private ProductInstanceService productInstanceService;
	
	@ModelAttribute("currentUser")
	public User initCurrentUser(Model model) {
		Object user = RequestContextHolder.currentRequestAttributes().getAttribute("currentUser", RequestAttributes.SCOPE_SESSION);
		if(user!=null){
			return (User)user;
		}else{
		    return User.createInitalUser(); 
		}
		
    }

	@RequestMapping(value = "/listProduct", method = {RequestMethod.GET, RequestMethod.POST})
	public String listProduct(@ModelAttribute("currentUser")User currentUser, Long categoryId, Page page, ProductSearchVo productVO, Model model) {
		if(currentUser==null){
			model.addAttribute("currentUser",User.createInitalUser());
		}
		List<Category> categoryList = null;
		List<Products> featuredProductList = null;
		List<Products> productList = null;
		
		if(page==null){
			page = new Page();
		}
		if(StringUtils.isBlank(page.getRequestType())){
			page.setRequestType("search");
		}
		
		try {
			if(categoryId!=null){
				productVO = new ProductSearchVo();
				productVO.getCategoryIds().add(categoryId);
				page = new Page();
				
				productList = productService.getAllListOfProductByConditions(productVO,page);
//						getProductListWithImage();
			}else{
				if(page==null){
					return "error";
				}
//				if(productVO==null){
//					productList = productService.getProductListWithImage();
//				}
				
				if("search".equalsIgnoreCase(page.getRequestType())){
					page.setCurrentPageNum(1);
				}else if("pagination".equalsIgnoreCase(page.getRequestType())){
					page.calculate();
				}
				productList = productService.getAllListOfProductByConditions(productVO,page);
				
				 
			}
			
			categoryList = categoryService.getAllListOfCategory();
			featuredProductList = productService.getProductListWithImage();
//			if(categoryId!=null){
//				productList = productService.getAllListOfProductByConditions(new ProductSearchVo(categoryId));
//			}
//			if(productList==null || productList.size()==0){
//				productList = featuredProductList;
//			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		model.addAttribute("page",page);
		model.addAttribute("productVO",productVO);
		model.addAttribute("categoryList",categoryList);
		model.addAttribute("productList",productList);
		model.addAttribute("featuredProductList",featuredProductList);
		model.addAttribute("pageLimitMap",Constant.getPageLimitMap());
		model.addAttribute("sortByMap",Constant.getSortByMap());
		return "productList";
	}
	

	@RequestMapping(value = "/productDetail", method = RequestMethod.GET)
	public String productDetail(@ModelAttribute("currentUser")User currentUser, Long productId, Model model) {
		if(currentUser==null){
			model.addAttribute("currentUser",User.createInitalUser());
		}
		List<Category> categoryList = null;
		List<Products> featuredProductList = null;
		
		List<Product_option_values> colorOptionValueList = new ArrayList<Product_option_values>();
		List<Product_option_values> sizeOptionValueList = new ArrayList<Product_option_values>();
		
		Products product = null;
		try {
			categoryList = categoryService.getAllListOfCategory();
			featuredProductList = productService.getProductListWithImage();
			if(productId!=null){
				product = productService.getProductWithImagesById(productId);
				List<Product_instance> productInstanceList = productInstanceService.getProductInstanceByProductId(productId);
				product.setProductInstanceList(productInstanceList);
				Map<Long, Product_option_values> colorMap = new HashMap<Long, Product_option_values>();
				Map<Long, Product_option_values> sizeMap = new HashMap<Long, Product_option_values>();
				int totalQuantity = 0;
				for(Product_instance productInstance : productInstanceList){
					colorMap.put(productInstance.getColorId(), productInstance.getColor_product_option_value());
					sizeMap.put(productInstance.getSizeId(), productInstance.getSize_product_option_value());
					totalQuantity = totalQuantity+productInstance.getQuantity();
				}
				for(Long key :colorMap.keySet()){
					colorOptionValueList.add(colorMap.get(key));
				}
				for(Long key :sizeMap.keySet()){
					sizeOptionValueList.add(sizeMap.get(key));
				}
				product.setTotalQuantityl(totalQuantity);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
		model.addAttribute("categoryList",categoryList);
		model.addAttribute("featuredProductList",featuredProductList);
		model.addAttribute("product",product);
		model.addAttribute("colorOptionValueList",colorOptionValueList);
		model.addAttribute("sizeOptionValueList",sizeOptionValueList);
		return "productDetail";
	}

	 
}
